
const queryString = require("querystring");
const request = require('./request');
module.exports = {
  // 根据 id 获取歌单详情
  index: async (query) => {
    const {id, raw} = query;
    if (!id) {
      return{
        result: 500,
        errMsg: 'id 不能为空',
      }
    }
    let data = {
      data: {
        type: 1,
        utf8: 1,
        disstid: id, // 歌单的id
        loginUin: 0,
      }
    };
    //console.log(data);
    const result = await request('http://c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg?'+queryString.stringify(data.data));
    if (Number(raw)) {
      return result;
    } else {
      const resData = {
        result: 100,
        data: result.cdlist[0] || {},
      };
      return resData;
    }

  },

  // 获取歌单分类
  category: async (query) => {
    const {raw} = query;

    const result = await request('https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_tag_conf.fcg?format=json&inCharset=utf8&outCharset=utf-8');

    if (Number(raw)) {
      return result;
    } else {
      return {
        result: 100,
        data: result.data.categories.map((item) => ({
          type: item.categoryGroupName,
          list: (item.items || []).map((obj) => ({
            id: obj.categoryId,
            name: obj.categoryName,
          }))
        })),
      }
    }
  },

  // 根据歌单分类筛选歌单
  list: async (query) => {
    const {raw, num = 20, pageSize = num, pageNo = 1, sort = 5, category = 10000000} = query;

    let data = {
      data: {
        inCharset: 'utf8',
        outCharset: 'utf-8',
        sortId: sort,
        categoryId: category,
        sin: pageSize * (pageNo - 1),
        ein: pageNo * pageSize - 1,
      }
    }

    const result = await request('https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_by_tag.fcg?'+queryString.stringify(data.data));
    //console.log(result)
    if (Number(raw)) {
      return result;
    } else {
      const {list = [], sortId, categoryId, ein, sum} = result.data;
      return {
        result: 100,
        data: {
          list,
          sort: sortId,
          category: categoryId,
          pageNo,
          pageSize,
          total: sum,
        }
      };
    }
  },


  // 这是一个把网易云歌单进行简单搬运到qq音乐的功能，运行的比较慢，看到的人感兴趣的就自己研究一下吧
  // '/move': async ({req, res, request}) => {
  //   const {id} = req.query;
  //   const url = `http://music.jsososo.com/api/playlist/detail?id=${id}`;
  //
  //   const result = await request(url);
  //   const list = [];
  //
  //   for (let index = 0; index < result.playlist.tracks.length; index++) {
  //     const item = result.playlist.tracks[index];
  //     const song = {
  //       name: item.name,
  //       artist: item.ar.map(a => a.name).join('/'),
  //       album: item.al.name,
  //     };
  //     const key = `${song.name} ${song.artist} ${song.album}`;
  //     try {
  //       const obj = await request(`http://music.jsososo.com/apiQ/song/find?key=${key}`);
  //       if (obj.data && obj.data.songmid) {
  //         list.push(obj.data.songmid);
  //         await request({
  //           url: `http://127.0.0.1:${global.PORT}/songlist/add`,
  //           data: {
  //             mid: obj.data.songmid,
  //             dirid: 201,
  //           }
  //         });
  //       }
  //     } catch (e) {
  //
  //     }
  //   }
  //
  //   res.send(list);
  // },
}
